package top.xphu.zip.entiy;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.zip.ZipOutputStream;

/**
 * 压缩文件实体类
 *
 * @author xphu
 * @version 1.0
 * @date 2021/7/2 15:45
 */
public class BasaltFile {
    /**
     * 压缩文件名称
     */
    private String zipFileName;
    /**
     * 压缩文件大小
     */
    private long zipFileSize;

    /**
     * 文件内容
     */
    private ZipOutputStream body;

    /**
     * 源文件流
     */
    private OutputStream sourceFileBody;

    /**
     * 临时文件
     */
    private File empFile;


    /**
     * 构建对象
     *
     * @return BasaltFile
     */
    public static BasaltFile buider() {
        return new BasaltFile();
    }


    /**
     * 最终创建的方法
     *
     * @return
     */
    public BasaltFile create() {
        // 创建文件保存目录
        String resources = System.getProperty("user.dir") + "\\temp\\";
        File resFile = new File(resources);
        if (!resFile.exists()) {
            resFile.mkdir();
        }

        // 创建临时文件对象
        File file = new File(resources + this.zipFileName);
        // 获取文件流
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            this.setEmpFile(file);
            this.setSize(file.length());
            this.setSourceFileBody(fos);
            this.setBody(new ZipOutputStream(fos));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public BasaltFile name(String name) {
        // 创建压缩文件名称
        if (StringUtils.isNotBlank(name)) {
            this.zipFileName = (name.lastIndexOf('.') != -1) ? name : (name + ".zip");
        } else {
            this.zipFileName = "temp" + (int) (Math.random() * 100) + ".zip";
        }
        return this;
    }

    public void close() {
        try {
            this.getBody().close();
            this.getSourceFileBody().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * get方法
     */

    public String getZipFileName() {
        return zipFileName;
    }

    public String getName() {
        return zipFileName;
    }

    public Long getSize() {
        return zipFileSize;
    }

    public ZipOutputStream getBody() {
        return body;
    }

    public File getEmpFile() {
        return empFile;
    }

    public OutputStream getSourceFileBody() {
        return sourceFileBody;
    }

    public long getZipFileSize() {
        return zipFileSize;
    }

    /**
     * set方法
     **/
    public void setZipFileSize(long zipFileSize) {
        this.zipFileSize = zipFileSize;
    }

    public void setZipFileName(String zipFileName) {
        this.zipFileName = zipFileName;
    }

    public void setSourceFileBody(OutputStream sourceFileBody) {
        this.sourceFileBody = sourceFileBody;
    }

    public void setName(String zipFileName) {
        this.zipFileName = zipFileName;
    }

    public void setSize(long zipFileSize) {
        this.zipFileSize = zipFileSize;
    }

    public void setBody(ZipOutputStream body) {
        this.body = body;
    }

    public void setEmpFile(File empFile) {
        this.empFile = empFile;
    }

}
